<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="com.library.dao.*, com.library.entity.*" %>
<%
    // 检查用户是否登录
    User user = (User) session.getAttribute("user");
    if (user == null || user.getRole() != 0) {
        response.sendRedirect("../login.jsp");
        return;
    }
    
    // 获取统计数据
    BookDAO bookDAO = new BookDAO();
    UserDAO userDAO = new UserDAO();
    BorrowRecordDAO recordDAO = new BorrowRecordDAO();
    
    int totalBooks = bookDAO.getAllBooks().size();
    int totalUsers = userDAO.getAllUsers().size();
    int borrowedBooks = recordDAO.getCurrentRecordsByUserId(-1).size();
    int overdueBooks = recordDAO.getOverdueRecords().size();
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>图书管理系统 - 管理员仪表盘</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }
        .header {
            background-color: #333;
            color: white;
            padding: 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .header h1 {
            margin: 0;
        }
        .header a {
            color: white;
            text-decoration: none;
            margin-left: 20px;
        }
        .sidebar {
            width: 200px;
            background-color: #444;
            color: white;
            height: 100vh;
            position: fixed;
            padding-top: 20px;
        }
        .sidebar ul {
            list-style-type: none;
            padding: 0;
            margin: 0;
        }
        .sidebar li {
            padding: 10px 20px;
        }
        .sidebar li:hover {
            background-color: #555;
        }
        .sidebar a {
            color: white;
            text-decoration: none;
        }
        .main-content {
            margin-left: 200px;
            padding: 20px;
        }
        .stats-container {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 20px;
        }
        .stat-card {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 200px;
        }
        .stat-title {
            font-size: 16px;
            color: #666;
        }
        .stat-value {
            font-size: 32px;
            font-weight: bold;
            margin-top: 10px;
        }
        .welcome {
            margin-bottom: 20px;
        }
        .welcome h2 {
            margin: 0;
        }
        .welcome p {
            color: #666;
        }
        .section {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
        .section h3 {
            margin-top: 0;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>图书管理系统</h1>
        <div>
            <span>欢迎，${user.username}</span>
            <a href="logout">退出登录</a>
        </div>
    </div>
    
    <div class="sidebar">
        <ul>
            <li><a href="dashboard.jsp">仪表盘</a></li>
            <li><a href="book?action=list">图书管理</a></li>
            <li><a href="user?action=list">用户管理</a></li>
            <li><a href="borrow?action=list">借阅管理</a></li>
            <li><a href="borrow?action=overdue">逾期管理</a></li>
        </ul>
    </div>
    
    <div class="main-content">
        <div class="welcome">
            <h2>管理员仪表盘</h2>
            <p>您好，${user.realName}！这里是图书管理系统的管理员控制台。</p>
        </div>
        
        <div class="stats-container">
            <div class="stat-card">
                <div class="stat-title">总图书数</div>
                <div class="stat-value"><%= totalBooks %></div>
            </div>
            <div class="stat-card">
                <div class="stat-title">总用户数</div>
                <div class="stat-value"><%= totalUsers %></div>
            </div>
            <div class="stat-card">
                <div class="stat-title">借出图书</div>
                <div class="stat-value"><%= borrowedBooks %></div>
            </div>
            <div class="stat-card">
                <div class="stat-title">逾期图书</div>
                <div class="stat-value"><%= overdueBooks %></div>
            </div>
        </div>
        
        <div class="section">
            <h3>最近借阅记录</h3>
            <table border="1" width="100%">
                <tr>
                    <th>借阅ID</th>
                    <th>用户</th>
                    <th>图书</th>
                    <th>借阅日期</th>
                    <th>应还日期</th>
                    <th>状态</th>
                </tr>
                <%
                    List<BorrowRecord> recentRecords = recordDAO.getAllRecords();
                    int count = 0;
                    for (BorrowRecord record : recentRecords) {
                        if (count >= 5) break;
                        
                        User borrower = userDAO.getUserById(record.getUserId());
                        Book book = bookDAO.getBookById(record.getBookId());
                %>
                <tr>
                    <td><%= record.getId() %></td>
                    <td><%= borrower.getUsername() %></td>
                    <td><%= book.getTitle() %></td>
                    <td><%= DateUtil.formatDateOnly(record.getBorrowDate()) %></td>
                    <td><%= DateUtil.formatDateOnly(record.getReturnDate()) %></td>
                    <td>
                        <% if (record.getStatus() == 0) { %>
                            借阅中
                        <% } else if (record.getStatus() == 1) { %>
                            已归还
                        <% } else { %>
                            逾期未还
                        <% } %>
                    </td>
                </tr>
                <%
                        count++;
                    }
                %>
            </table>
        </div>
        
        <div class="section">
            <h3>系统信息</h3>
            <p>服务器时间：<%= new java.util.Date() %></p>
            <p>系统版本：1.0</p>
            <p>上次更新：2023-05-01</p>
        </div>
    </div>
</body>
</html>
